package cn.sciento.boot.data.change.autoconfigure;

import io.choerodon.mybatis.pagehelper.Dialect;
import java.util.List;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import cn.sciento.boot.data.change.handler.DataChangeBaseHandler;
import cn.sciento.boot.data.change.intercept.DataChangeInterceptor;
import cn.sciento.boot.data.change.intercept.DataCommitInterceptor;
import cn.sciento.boot.data.change.selector.manager.EntityTableSelectorManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(prefix = "stong.data.change", name = {"enable"}, havingValue = "true")
@EnableConfigurationProperties({DataChangeProperty.class})
public class DataChangeConfiguration {
  private static final Logger LOGGER = LoggerFactory.getLogger(DataChangeConfiguration.class);
  
  DataChangeConfiguration(SqlSessionFactory sqlSessionFactory, Dialect dialect, List<DataChangeBaseHandler> dataChangeFilters, EntityTableSelectorManager entityTableSelectorManager) {
    DataChangeInterceptor dataChangeInterceptor = new DataChangeInterceptor(dataChangeFilters, entityTableSelectorManager);
    sqlSessionFactory.getConfiguration().addInterceptor((Interceptor)dataChangeInterceptor);
    DataCommitInterceptor dataCommitInterceptor = new DataCommitInterceptor(dataChangeFilters);
    sqlSessionFactory.getConfiguration().addInterceptor((Interceptor)dataCommitInterceptor);
  }
}
